﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using RestaurantBooker.Web.Services;
using RestaurantBooker.Web.Data;
using System.ServiceModel.DomainServices.Client;



namespace RestaurantBooker.View
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
#if DEBUG
            tbIsDebugvVersion.Visibility = Visibility.Visible;
#endif
        }

        RestaurantBookerDomainContext _context = new RestaurantBookerDomainContext();

        private void button1_Click(object sender, RoutedEventArgs e)
        {            
            EntityQuery<Booker> query = _context.GetBookersWithRestaurantAndRoomsQuery();
            
            
            _context.Load(query, (op)  =>
            {
                Booker booker  = op.Entities.FirstOrDefault();

                if (booker.Restaurant.Rooms.Count >0)
                    MessageBox.Show(booker.Restaurant.Rooms.First().room_name);    
            }, null);



            //var restorantQueary = context.GetRestorantsWithRoomsQuery();
            //context.Load(restorantQueary, (op) =>
            //{
            //    foreach (Restaurant rests in op.Entities)
            //    {
            //        if (rests.Rooms != null && rests.Rooms.Count > 0)
            //        {
            //            var room = rests.Rooms.First();
            //            if (room != null)
            //                MessageBox.Show(room.room_name);
            //            else MessageBox.Show("Room is NULL");
            //        }
            //    }                
            //}, null);
        }

        private void restaurantDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void bookerDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            MessageDlg.ShowError("Test ShowError ");

            MessageBox.Show("Test standart MessageBox");

            var newRest = new Restaurant(){
                restaurant_name = "New restaraunt 3",                
            };
            _context.Restaurants.Add(newRest);
            _context.SubmitChanges((so) =>
                {
                    if (so.Error == null)
                    {
                        //newRest.restaurant_id Have initialized value
                        //so.UserState as Restaurant
                    }
                    else
                     MessageDlg.ShowError("Error add new restaraunt " + Environment.NewLine + so.Error.ToString());
 
                }, newRest);
        }

        private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            //ToDO: need implement
        }

        private void Thumb_MouseMove(object sender, MouseEventArgs e)
        {
            //ToDO: need implement
        }
    }
}
